package pe.com.bbva.modpre.dominio;

import javax.persistence.*;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import pe.com.bbva.modpre.dominio.util.AuditoriaBean;


/**
 * The persistent class for the SEG_PERMISO database table.
 * 
 */
@Entity
@Table(name="SEG_PERMISO")
@SuppressWarnings("serial")
public class Permiso extends AuditoriaBean {

	@Id
	@Column(unique=true, nullable=false, precision=16)
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQ_PERMISO")
	@TableGenerator(name = "SEQ_PERMISO", table = "SEQ_ENTIDAD", pkColumnName = "ENTIDAD", initialValue = 1,
	valueColumnName = "ULTIMO_ID", pkColumnValue = "pe.com.bbva.modpre.dominio.Permiso", allocationSize = 1)
	private Long id;

	//uni-directional many-to-one association to Valor
	@ManyToOne
	@JoinColumn(name="ESTADO", nullable=false)
	private Valor estado;

	//uni-directional many-to-one association to Opcion
	@ManyToOne
	@JoinColumn(name="OPCION", nullable=false)
	private Opcion opcion;

	//uni-directional many-to-one association to Rol
	@ManyToOne
	@JoinColumn(name="ROL", nullable=false)
	private Rol rol;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Valor getEstado() {
		return estado;
	}

	public void setEstado(Valor estado) {
		this.estado = estado;
	}

	public Opcion getOpcion() {
		return opcion;
	}

	public void setOpcion(Opcion opcion) {
		this.opcion = opcion;
	}

	public Rol getRol() {
		return rol;
	}

	public void setRol(Rol rol) {
		this.rol = rol;
	}

	@Override
	public int hashCode() {
		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(5, 31);
		if(rol != null && opcion != null)
		{
			hashCodeBuilder.append(rol.getCodigo());
			hashCodeBuilder.append(opcion.getCodigo());
		}
		else
		{
			hashCodeBuilder.append(getId());
		}
		return hashCodeBuilder.toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null)
		{
			return false;
		}
		if (obj instanceof Permiso) {
			Permiso bean = (Permiso) obj;
			EqualsBuilder equalsBuilder = new EqualsBuilder();
			if(rol != null && opcion != null && bean.rol != null && bean.opcion != null)
			{
				equalsBuilder.append(rol.getId(), bean.rol.getId());
				equalsBuilder.append(opcion.getId(), bean.opcion.getId());
			}
			else
			{
				equalsBuilder.append(getId(), bean.getId());
			}
			return equalsBuilder.isEquals();
			
		}
		return false;
	}
	
}